package com.txl.cn.spark02

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

/**
  * Created by txl on 2017/12/27.
  */
object SparkTest {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("SparkTest").setMaster("local[*]")
    val sc = new SparkContext(conf)
    val lines = sc.textFile(args(0))
    val words: RDD[String] = lines.flatMap(_.split(" "))
    val wordWithOne: RDD[(String, Int)] = words.map((_,1))
    val grouped: RDD[(String, Iterable[(String, Int)])] = wordWithOne.groupBy(_._1)
    val res: RDD[(String, Int)] = grouped.map({
      t =>
        val v = t._2.map(x => x._2).sum
        (t._1, v)
    })
    val res2=res.sortBy(- _._2).collect()
    //val res2=res.sortBy(- _._2)
    res2.foreach(println)

  }

}
